package com.lanou.musicbox.adapter.seekmusic;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.lanou.musicbox.R;
import com.lanou.musicbox.bean.MV;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 15/10/27.
 * 进入搜索_MV页面Adapter
 */
public class EnterTheSeekMusic_MVAdapter extends BaseAdapter {
    private List<MV.DataEntity> data;
    private Context mContext;
    private VolleySingleton volleySingleton;
    private ImageLoader imageLoader;

    public EnterTheSeekMusic_MVAdapter(List<MV.DataEntity> data, Context mContext) {
        this.data = data;
        this.mContext = mContext;
        volleySingleton = VolleySingleton.getVolleySingleton(mContext);
        imageLoader = volleySingleton.getImageLoader();
    }

    @Override
    public int getCount() {
        return data != null && data.size() > 0 ? data.size() : 0;
    }

    @Override
    public Object getItem(int i) {
        return data != null && data.size() > 0 ? data.get(i) : null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.entertheseekmusic_mv_item, null);
            holder = new ViewHolder();
            holder.picUrl = (ImageView) view.findViewById(R.id.picUrl);
            holder.videoName = (TextView) view.findViewById(R.id.videoName);
            holder.singerName = (TextView) view.findViewById(R.id.singerName);
            holder.pickCount = (TextView) view.findViewById(R.id.pickCount);
            holder.bulletCount = (TextView) view.findViewById(R.id.bulletCount);
            holder.duration = (TextView) view.findViewById(R.id.duration);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        MV.DataEntity mv = data.get(i);
        ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.picUrl, R.mipmap.loading, R.mipmap.loading);
        imageLoader.get(mv.getPicUrl(), listener);
        if (mv != null) {
            holder.videoName.setText(mv.getVideoName());
            if (mv.getSingerName() != null) {
                holder.singerName.setText(mv.getSingerName() + "");
            }
            holder.pickCount.setText(mv.getPickCount() + "");
            holder.bulletCount.setText(mv.getBulletCount() + "");
            holder.duration.setText(mmToMin(mv.getMvList().get(0).getDuration()));
        }
        return view;
    }

    private class ViewHolder {
        private ImageView picUrl;
        private TextView videoName, singerName, pickCount, bulletCount, duration;
    }

    // 设置时间的方法
    public String mmToMin(int mm) {
        int min = 0;
        int sec = 0;
        String sMin = "";
        String mSec = "";

        sec = mm / 1000;//一共多少秒
        min = sec / 60;//一共多少分钟
        sec %= 60; //还剩多少秒
        if (min < 10) {
            sMin = "0" + min;
        } else {
            sMin = String.valueOf(min);
        }

        if (sec < 10) {
            mSec = "0" + sec;
        } else {
            mSec = String.valueOf(sec);
        }
        return sMin + ":" + mSec;
    }
}
